package com.lanyun.oss.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.lanyun.oss.entity.File;
import com.lanyun.oss.dao.FileDao;
import com.lanyun.oss.service.FileService;
import com.lanyun.web.enums.IsDeletedFlagEnum;
import com.lanyun.web.enums.IsEnabledFlagEnum;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 文件表，用于存储文件的基本信息(File)表服务实现类
 *
 * @author makejava
 * @since 2025-03-16 15:28:50
 */
@Service("fileService")
public class FileServiceImpl implements FileService {
    @Resource
    private FileDao fileDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public File queryById(Long id) {
        return this.fileDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param
     * @param productId
     * @return 实例对象
     */
    @Override
    public void insert(MultipartFile file, String filePath, String productId, String detailType) {
        File fileEntity = new File();
        fileEntity.setFileName(file.getOriginalFilename());
        fileEntity.setFileType(file.getContentType());
        fileEntity.setFileSize((int) file.getSize());
        fileEntity.setFilePath(filePath);
        fileEntity.setProductId(productId);
        fileEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());
        fileEntity.setDetailType(detailType);
        if (detailType.equals("banner")) {
            fileEntity.setIsBanner(IsEnabledFlagEnum.ENABLED.getVal());
            fileEntity.setSortOrder(1);
        }
        this.fileDao.insert(fileEntity);
    }

    /**
     * 修改数据
     *
     * @param file 实例对象
     * @return 实例对象
     */
    @Override
    public File update(File file) {
        this.fileDao.update(file);
        return this.queryById(file.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.fileDao.deleteById(id) > 0;
    }

    @Override
    public void saveFile(MultipartFile file, String filePath, String productId, String detailType) {
        File fileEntity = new File();
        fileEntity.setFileName(file.getOriginalFilename());
        fileEntity.setFileType(file.getContentType());
        fileEntity.setFileSize((int) file.getSize());
        fileEntity.setFilePath(filePath);
        fileEntity.setDetailType(detailType);
        fileEntity.setProductId(productId);
        fileEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());
        fileEntity.setIsBanner(IsEnabledFlagEnum.DISABLED.getVal());
        fileDao.insert(fileEntity);
    }

    @Override
    public Boolean delete(File file) {
        File fileEntity = new File();
        fileEntity.setFilePath(file.getFilePath());
        File fileResult = this.fileDao.queryByCondition(file);
        if (ObjectUtil.isNotNull(fileResult)) {
            fileResult.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
        }
        return ObjectUtil.isNotNull(this.update(fileResult));
    }


}
